package com.edisoncgh.controller;

import com.edisoncgh.pojo.Book;
import com.edisoncgh.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/book")
public class BookController {
    // controller调service层
    @Autowired
    @Qualifier("BookServiceImpl")
    private BookService bookService;

    // 查询全部书籍，返回一个书籍展示页面
    @RequestMapping("/allBook")
    public String list(Model model) {
        List<Book> list = bookService.findAllBook();
        model.addAttribute("list", list);
        return "allBook";
    }

    // 跳转到新增书籍页面
    @RequestMapping("/toAddBook")
    public String toAddPaper() {
        return "addBook";
    }

    // 新增书籍的请求
    @RequestMapping("/addBook")
    public String addPaper(Book book) {
        bookService.addBook(book);
        return "redirect:/book/allBook";
    }

    // 跳转到修改页面
    @RequestMapping("/toUpdate")
    public  String toUpdatePage(String ISBN,Model model) {
        Book book = bookService.findBookById(ISBN);
        model.addAttribute("QBook", book);
        return "updateBook";
    }

    // 修改书籍
    @RequestMapping("/updateBook")
    public String updateBook(Book book) {
        boolean feedback = bookService.updateBook(book);
        if (feedback) {
            System.out.println("添加成功:"+book);
        }
        return "redirect:/book/allBook";
    }

    // 删除书籍
    @RequestMapping("/deleteBook")
    public String deleteBook(String ISBN) {
        bookService.deleteBookById(ISBN);
        return "redirect:/book/allBook";
    }
}
